<?php
namespace \DragonU\DB;
/**
 * CorePress DB Debug library.
 *
 * @internal
 * Copyright (c) 2009, Jacob Santos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the DragonU nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @author Jacob Santos <wordpress@santosj.name>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
 * @package DragonU
 * @subpackage DB
 * @since 0.1
 */

/**
 * CorePress DB Debug class.
 *
 * Handles all of the debugging for the database queries and calls.
 *
 * @author Jacob Santos
 * @since 0.1
 * @package DragonU
 * @subpackage DB
 */
class Debug
{

	/**
	 * Whether to suppress errors from bothering the user.
	 *
	 * @since 0.1
	 * @var bool
	 */
	protected $suppressErrors = true;

	/**
	 * Whether to show errors.
	 *
	 * @since 0.1
	 * @var bool
	 */
	protected $showErrors = false;

	/**
	 * Whether to enable debugging.
	 *
	 * @since 0.1
	 * @var bool
	 */
	protected $isDebugging = false;

	/**
	 * Does nothing.
	 *
	 * @since 0.1
	 */
	public function __construct()
	{

	}

	/**
	 * Enable debugging and retrieve previous value of the enabled state.
	 *
	 * @since 0.1
	 *
	 * @return bool The previous state of whether debugging was enabled or disabled.
	 */
	public function enable()
	{
		$previous = $this->isDebugging;

		$this->isDebugging = true;

		return $previous;
	}

	/**
	 * Disable debugging and retrieve previous value of the enabled state.
	 *
	 * @since 0.1
	 *
	 * @return bool The previous state of whether debugging was enabled or disabled.
	 */
	public function disable()
	{
		$previous = $this->isDebugging;

		$this->isDebugging = false;

		return $previous;
	}

	public function errors($state)
	{
		switch( $state )
		{
			case 'show':
				$this->showErrors = true;
				break;
			case 'hide':
				$this->showErrors = false;
				break;
			case 'suppress':
				$this->suppressErrors = true;
				break;
			case 'unsuppress':
				$this->suppressErrors = false;
				break;
			default:
				return false;
		}

		return true;
	}

	/**
	 * Retrieve the function or method which called the query.
	 *
	 * The caller information is in an array with 'file', 'caller', and
	 * 'line' indices. The caller will be the method or function name that was
	 * guessed to be accurate. The file and line indices are exactly what the
	 * name applies.
	 *
	 * @since 0.1
	 * @todo Complete this with debugging as well.
	 *
	 * @return array Method or function name, with line and file.
	 */
	public function caller()
	{

	}

	/**
	 * Log or display error depending on the settings.
	 *
	 * @since 0.1
	 *
	 * @param string $message Error message for database execution.
	 */
	public function error($message)
	{
		/** @todo Use the logging class to log message */

		if ( $this->suppressErrors === true )
		{
			return;
		}

		if ( $this->showErrors === true )
		{
			echo '<div class="db-error">'.$message.'</div>'."\n\n";
		}
	}

}